//
//  RadioDetailViewController.m
//  Leisure
//
//  Created by 大展 on 16/7/5.
//  Copyright © 2016年 大展. All rights reserved.
//

#import "RadioDetailViewController.h"

@interface RadioDetailViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataSource;

@end

@implementation RadioDetailViewController

- (NSMutableArray *)dataSource {

    if (!_dataSource) {

        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.tableView registerNib:[UINib nibWithNibName:@"RadioDetailCell" bundle:nil] forCellReuseIdentifier:@"RadioDetailModel_cell"];
    // 头视图
    UIImageView *headerView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight/3)];
    headerView.backgroundColor = [UIColor redColor];

    [RequestManager fecthDataWithUrl:kRadioDetailUrl parameter:@{@"radioid":self.radioID} mehtod:@"POST" compelet:^(NSData *data) {

        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"%@",bigDic);

        [headerView sd_setImageWithURL:[NSURL URLWithString:bigDic[@"data"][@"radioInfo"][@"coverimg"]]];

        for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {

            RadioDetailModel *model = [[RadioDetailModel alloc]init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.dataSource addObject:model];
        }
    } updateUI:^{

        self.tableView.tableHeaderView = headerView;
        [self.tableView reloadData];
    }];
}

#pragma mark TableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    BaseCell *cell = [FactoryMode productCellWithModel:self.dataSource[indexPath.row] tableView:tableView indexPath:indexPath];

//    cell.textLabel.text = [self.dataSource[indexPath.row] title];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 150;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    RadioPlayViewController *radioPlayVC = [RadioPlayViewController sharedManager];

    radioPlayVC.index = indexPath.row;
    radioPlayVC.dataSource = self.dataSource;

    [self.navigationController pushViewController:radioPlayVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
